package fi.jamk.ourcookingapp.activity;

import java.util.List;
import java.util.concurrent.ExecutionException;

import org.json.JSONException;

import android.app.ListActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AbsListView;
import android.widget.AbsListView.OnScrollListener;
import android.widget.ListView;
import android.widget.Toast;
import fi.jamk.ourcookingapp.R;
import fi.jamk.ourcookingapp.adapter.MyReceiptAdapter;
import fi.jamk.ourcookingapp.backend.architecture.manager.ReceiptManager;
import fi.jamk.ourcookingapp.backend.exceptions.EmptySearchException;
import fi.jamk.ourcookingapp.backend.exceptions.NoIngredientsException;
import fi.jamk.ourcookingapp.backend.exceptions.NoReceiptException;
import fi.jamk.ourcookingapp.backend.exceptions.NoStepsException;
import fi.jamk.ourcookingapp.backend.model.Receipt;

/**
 * This activity is for showing the stream where the receipts are, by default, sorted by creation date
 * @author Marius
 */
public class StreamActivity extends ListActivity implements OnScrollListener {
	
	private int count;
	private int limitCount;
	
	private List<Receipt> filter;
	private MyReceiptAdapter adapter;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
	}

	@Override
	protected void onListItemClick(ListView l, View v, int position, long id) {
		Receipt rec = (Receipt)getListAdapter().getItem(position);
		// checking whether chosen receipt's user is the current user
		if(rec.getUser().getID() == AppActivity.facade.getCurrentUser().getID()){
			// go to add/edit view
			Intent detailIntent = new Intent(this, AddReceiptActivity.class);
			detailIntent.putExtra(ReceiptDetailActivity.ARG_ITEM, rec);
			detailIntent.putExtra(ReceiptDetailActivity.SOURCE, ReceiptDetailActivity.STREAM);
			startActivity(detailIntent);
		} else {
			// go to details view
			rec.setCountClicks(rec.getCountClicks()+1);
			try {
				AppActivity.facade.updateReceipt(rec, true);
			} catch (InterruptedException | ExecutionException | JSONException | NoIngredientsException | NoStepsException e) {
				Toast.makeText(getApplicationContext(), e.getMessage(), Toast.LENGTH_LONG).show();
			}
			Intent detailIntent = new Intent(this, ReceiptDetailActivity.class);
			detailIntent.putExtra(ReceiptDetailActivity.ARG_ITEM, rec);
			detailIntent.putExtra(ReceiptDetailActivity.SOURCE, ReceiptDetailActivity.STREAM);
			startActivity(detailIntent);
		}
	}

	@Override
	protected void onResume() {
		super.onResume();
		try {
			AppActivity.facade.setFilter(ReceiptManager.LIMIT_KEY, 0);
			filter = AppActivity.facade.filter(false);
			adapter = new MyReceiptAdapter(this, R.layout.receipt_list_item, filter);
			setListAdapter(adapter);
			count = 1;
			limitCount = 0;
		} catch (Exception e) {
			e.printStackTrace();
		}
		getListView().setOnScrollListener(this);
	}
	
	@Override
	public void onScroll(AbsListView view, int firstVisibleItem,
			int visibleItemCount, int totalItemCount) {
		if(firstVisibleItem == count){
			try {
				limitCount++;
				AppActivity.facade.setFilter(ReceiptManager.LIMIT_KEY, ReceiptManager.LIMIT * limitCount);
				adapter.addAll(AppActivity.facade.filter(false));
				adapter.notifyDataSetChanged();
				count += 7;
			} catch (EmptySearchException | NoReceiptException | InterruptedException | ExecutionException | JSONException e) {
				e.printStackTrace();
			}
		}
	}

	@Override
	public void onScrollStateChanged(AbsListView view, int scrollState) {
	}
	
}
